package com.wctj.api.web.controller;

import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import cloud.tianai.captcha.spring.plugins.secondary.SecondaryVerificationApplication;
import com.wctj.api.base.constant.JwtConstant;
import com.wctj.api.web.controller.dto.captcha.CaptchaDataDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 放开后台用户管理 Controller
 * @author carol
 */
@RestController
@Api(tags = "OpenapiUserController", description = "开放后台用户管理")
@RequestMapping("/openapi")
public class OpenapiUserController {
    @Resource
    private ImageCaptchaApplication imageCaptchaApplication;


    @ApiOperation("生成拖拽验证码")
    @PostMapping("/gen")
    public CaptchaResponse<ImageCaptchaVO> genCaptcha(@RequestParam(value = "type", required = false)String type,
                                                      HttpServletRequest request,
                                                      HttpServletResponse response) {
        if (StringUtils.isBlank(type)) {
            type = CaptchaTypeConstant.SLIDER;
        }
        if ("RANDOM".equals(type)) {
            int i = ThreadLocalRandom.current().nextInt(0, 4);
            if (i == 0) {
                // 滑块
                type = CaptchaTypeConstant.SLIDER;
            } else if (i == 1) {
                // 滑动还原
                type = CaptchaTypeConstant.CONCAT;
            } else if (i == 2) {
                // 旋转
                type = CaptchaTypeConstant.ROTATE;
            } else{
                // 点选
                type = CaptchaTypeConstant.WORD_IMAGE_CLICK;
            }

        }
        CaptchaResponse<ImageCaptchaVO> captchaResponse = imageCaptchaApplication.generateCaptcha(type);
        if (StringUtils.isNotBlank(captchaResponse.getId())){
            // 设置响应头
            response.setHeader(JwtConstant.HEADER_X_CAPTCHAID, captchaResponse.getId());
        }
        return captchaResponse;
    }

    @ApiOperation("拖拽验证码,校验第一次")
    @PostMapping("/check")
    public ApiResponse<?> checkCaptcha(@RequestBody CaptchaDataDTO data, HttpServletRequest request) {
        ApiResponse<?> response = imageCaptchaApplication.matching(data.getId(), data.getData());
        if (response.isSuccess()) {
            return ApiResponse.ofSuccess(Collections.singletonMap("id", data.getId()));
        }
        return response;
    }


    @ApiOperation("二次验证，一般用于机器内部调用，这里为了方便测试")
    @GetMapping("/check2")
    public boolean check2Captcha(@RequestParam("id") String id) {
        // 如果开启了二次验证
        if (imageCaptchaApplication instanceof SecondaryVerificationApplication) {
            return ((SecondaryVerificationApplication) imageCaptchaApplication).secondaryVerification(id);
        }
        return false;
    }

}
